package com.ruoyi.mall.pms.service.impl;

import com.ruoyi.mall.cms.domain.CmsSubjectProductRelation;
import com.ruoyi.mall.cms.mapper.CmsSubjectProductRelationMapper;
import com.ruoyi.mall.pms.domain.*;
import com.ruoyi.mall.pms.domain.dto.PmsProductDto;
import com.ruoyi.mall.pms.mapper.*;
import com.ruoyi.mall.pms.service.IPmsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品Service业务层处理
 *
 * @author xuxing
 * @date 2021-02-03
 */
@Service
public class PmsProductServiceImpl implements IPmsProductService
{
    @Autowired
    private PmsProductMapper pmsProductMapper;

    @Autowired
    private PmsMemberPriceMapper pmsMemberPriceMapper;

    @Autowired
    private PmsProductLadderMapper pmsProductLadderMapper;

    @Autowired
    private PmsProductFullReductionMapper pmsProductFullReductionMapper;

    @Autowired
    private PmsProductAttributeValueMapper pmsProductAttributeValueMapper;

    @Autowired
    private PmsProductSkuMapper pmsProductSkuMapper;

    @Autowired
    private CmsSubjectProductRelationMapper cmsSubjectProductRelationMapper;

    /**
     * 查询商品
     *
     * @param id 商品ID
     * @param detail 是否需要详细信息
     * @return 商品
     */
    @Override
    public PmsProductDto selectPmsProductById(Long id, Boolean detail) {
        PmsProductDto pmsProductDto = new PmsProductDto();
        PmsProduct pmsProduct = pmsProductMapper.selectPmsProductById(id);
        if (pmsProduct == null) {
            return null;
        }
        pmsProductDto.setProduct(pmsProduct);
        if (detail == null || !detail) {
            return pmsProductDto;
        }
        Long productId = pmsProduct.getId();
        if (pmsProduct.getPromotionType() == 2) {
            PmsMemberPrice pmsMemberPrice = new PmsMemberPrice();
            pmsMemberPrice.setProductId(productId);
            List<PmsMemberPrice> memberPriceList = pmsMemberPriceMapper.selectPmsMemberPriceList(pmsMemberPrice);
            pmsProductDto.setMemberPriceList(memberPriceList);
        } else if (pmsProduct.getPromotionType() == 3) {
            PmsProductLadder pmsProductLadder = new PmsProductLadder();
            pmsProductLadder.setProductId(productId);
            List<PmsProductLadder> ladderList = pmsProductLadderMapper.selectPmsProductLadderList(pmsProductLadder);
            pmsProductDto.setLadderList(ladderList);
        } else if (pmsProduct.getPromotionType() == 4) {
            PmsProductFullReduction pmsProductFullReduction = new PmsProductFullReduction();
            pmsProductFullReduction.setProductId(productId);
            List<PmsProductFullReduction> fullReductionList = pmsProductFullReductionMapper.selectPmsProductFullReductionList(pmsProductFullReduction);
            pmsProductDto.setFullReductionList(fullReductionList);
        }
        PmsProductAttributeValue pmsProductAttributeValue = new PmsProductAttributeValue();
        pmsProductAttributeValue.setProductId(productId);
        List<PmsProductAttributeValue> attributeValueList = pmsProductAttributeValueMapper.selectPmsProductAttributeValueList(pmsProductAttributeValue);
        pmsProductDto.setAttributeValueList(attributeValueList);
        PmsProductSku pmsProductSku = new PmsProductSku();
        pmsProductSku.setProductId(productId);
        List<PmsProductSku> productSkuList = pmsProductSkuMapper.selectPmsProductSkuList(pmsProductSku);
        pmsProductDto.setProductSkuList(productSkuList);
        CmsSubjectProductRelation cmsSubjectProductRelation = new CmsSubjectProductRelation();
        cmsSubjectProductRelation.setProductId(productId);
        List<Long> subjectIds = cmsSubjectProductRelationMapper.selectCmsSubjectProductRelationList(cmsSubjectProductRelation)
                .stream().map(CmsSubjectProductRelation::getSubjectId).collect(Collectors.toList());
        pmsProductDto.setSubjectIds(subjectIds);
        return pmsProductDto;
    }

    /**
     * 查询商品列表
     *
     * @param pmsProduct 商品
     * @return 商品
     */
    @Override
    public List<PmsProduct> selectPmsProductList(PmsProduct pmsProduct)
    {
        return pmsProductMapper.selectPmsProductList(pmsProduct);
    }

    /**
     * 新增商品
     *
     * @param pmsProduct 商品
     * @param memberPriceList 会员价格
     * @param ladderList 阶梯价格
     * @param fullReductionList 满减价格
     * @param attributeValueList 商品属性
     * @param productSkuList 商品sku
     * @param subjectIds 专题ids
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPmsProduct(PmsProduct pmsProduct, List<PmsMemberPrice> memberPriceList, List<PmsProductLadder> ladderList, List<PmsProductFullReduction> fullReductionList, List<PmsProductAttributeValue> attributeValueList, List<PmsProductSku> productSkuList, List<Long> subjectIds)
    {
        if (pmsProduct.getPromotionType() == 0) {
            pmsProduct.setPromotionStartTime(null);
            pmsProduct.setPromotionEndTime(null);
            pmsProduct.setPromotionPerLimit(null);
            pmsProduct.setPromotionPrice(null);
        }
        int row = pmsProductMapper.insertPmsProduct(pmsProduct);
        if (pmsProduct.getPromotionType() == 2) {
            memberPriceList.forEach(item -> {
                item.setProductId(pmsProduct.getId());
                pmsMemberPriceMapper.insertPmsMemberPrice(item);
            });
        } else if (pmsProduct.getPromotionType() == 3) {
            ladderList.forEach(item -> {
                System.out.println(item);
                item.setProductId(pmsProduct.getId());
                pmsProductLadderMapper.insertPmsProductLadder(item);
            });
        } else if (pmsProduct.getPromotionType() == 4) {
            fullReductionList.forEach(item -> {
                item.setProductId(pmsProduct.getId());
                pmsProductFullReductionMapper.insertPmsProductFullReduction(item);
            });
        }
        attributeValueList.forEach(item -> {
            item.setProductId(pmsProduct.getId());
            pmsProductAttributeValueMapper.insertPmsProductAttributeValue(item);
        });
        productSkuList.forEach(item -> {
            item.setProductId(pmsProduct.getId());
            pmsProductSkuMapper.insertPmsProductSku(item);
        });
        subjectIds.forEach(subjectId -> {
            CmsSubjectProductRelation cmsSubjectProductRelation = new CmsSubjectProductRelation();
            cmsSubjectProductRelation.setSubjectId(subjectId);
            cmsSubjectProductRelation.setProductId(pmsProduct.getId());
            cmsSubjectProductRelationMapper.insertCmsSubjectProductRelation(cmsSubjectProductRelation);
        });
        return row;
    }

    /**
     * 修改商品
     *
     * @param pmsProductDto 商品
     * @return 结果
     */
    @Override
    @Transactional
    public int updatePmsProduct(PmsProductDto pmsProductDto)
    {
        PmsProduct product = pmsProductDto.getProduct();
        List<PmsMemberPrice> memberPriceList = pmsProductDto.getMemberPriceList();
        List<PmsProductLadder> ladderList = pmsProductDto.getLadderList();
        List<PmsProductFullReduction> fullReductionList = pmsProductDto.getFullReductionList();
        List<PmsProductAttributeValue> attributeValueList = pmsProductDto.getAttributeValueList();
        List<PmsProductSku> productSkuList = pmsProductDto.getProductSkuList();
        List<Long> subjectIds = pmsProductDto.getSubjectIds();
        PmsProduct pmsProduct = pmsProductMapper.selectPmsProductById(product.getId());
        if (pmsProduct.getPromotionType() == 2) {
            PmsMemberPrice pmsMemberPrice = new PmsMemberPrice();
            pmsMemberPrice.setProductId(pmsProduct.getId());
            Long[] ids = pmsMemberPriceMapper.selectPmsMemberPriceList(pmsMemberPrice)
                    .stream().map(PmsMemberPrice::getId).toArray(Long[]::new);
            if (ids.length > 0) {
                pmsMemberPriceMapper.deletePmsMemberPriceByIds(ids);
            }
        } else if (pmsProduct.getPromotionType() == 3) {
            PmsProductLadder pmsProductLadder = new PmsProductLadder();
            pmsProductLadder.setProductId(pmsProduct.getId());
            Long[] ids = pmsProductLadderMapper.selectPmsProductLadderList(pmsProductLadder)
                    .stream().map(PmsProductLadder::getId).toArray(Long[]::new);
            if (ids.length > 0) {
                pmsProductLadderMapper.deletePmsProductLadderByIds(ids);
            }
        } else if (pmsProduct.getPromotionType() == 4) {
            PmsProductFullReduction pmsProductFullReduction = new PmsProductFullReduction();
            pmsProductFullReduction.setProductId(pmsProduct.getId());
            Long[] ids = pmsProductFullReductionMapper.selectPmsProductFullReductionList(pmsProductFullReduction)
                    .stream().map(PmsProductFullReduction::getId).toArray(Long[]::new);
            if (ids.length > 0) {
                pmsProductFullReductionMapper.deletePmsProductFullReductionByIds(ids);
            }
        }
        if (product.getPromotionType() == 0) {
            product.setPromotionStartTime(null);
            product.setPromotionEndTime(null);
            product.setPromotionPerLimit(null);
            product.setPromotionPrice(null);
        }
        if (product.getPromotionType() == 2) {
            memberPriceList.forEach(item -> {
                item.setProductId(product.getId());
                pmsMemberPriceMapper.insertPmsMemberPrice(item);
            });
        } else if (product.getPromotionType() == 3) {
            ladderList.forEach(item -> {
                System.out.println(item);
                item.setProductId(product.getId());
                pmsProductLadderMapper.insertPmsProductLadder(item);
            });
        } else if (product.getPromotionType() == 4) {
            fullReductionList.forEach(item -> {
                item.setProductId(product.getId());
                pmsProductFullReductionMapper.insertPmsProductFullReduction(item);
            });
        }
        PmsProductAttributeValue pmsProductAttributeValue = new PmsProductAttributeValue();
        pmsProductAttributeValue.setProductId(product.getId());
        Long[] attrIds = pmsProductAttributeValueMapper.selectPmsProductAttributeValueList(pmsProductAttributeValue)
                .stream().map(PmsProductAttributeValue::getId).toArray(Long[]::new);
        if (attrIds.length > 0) {
            pmsProductAttributeValueMapper.deletePmsProductAttributeValueByIds(attrIds);
        }
        attributeValueList.forEach(item -> {
            item.setProductId(product.getId());
            pmsProductAttributeValueMapper.insertPmsProductAttributeValue(item);
        });
        PmsProductSku pmsProductSku = new PmsProductSku();
        pmsProductSku.setProductId(product.getId());
        Long[] skuIds = pmsProductSkuMapper.selectPmsProductSkuList(pmsProductSku)
                .stream().map(PmsProductSku::getId).toArray(Long[]::new);
        if (skuIds.length > 0) {
            pmsProductSkuMapper.deletePmsProductSkuByIds(skuIds);
        }
        productSkuList.forEach(item -> {
            item.setProductId(product.getId());
            pmsProductSkuMapper.insertPmsProductSku(item);
        });
        CmsSubjectProductRelation subjectProductRelation = new CmsSubjectProductRelation();
        subjectProductRelation.setProductId(product.getId());
        Long[] ids = cmsSubjectProductRelationMapper.selectCmsSubjectProductRelationList(subjectProductRelation)
                .stream().map(CmsSubjectProductRelation::getId).toArray(Long[]::new);
        if (ids.length > 0) {
            cmsSubjectProductRelationMapper.deleteCmsSubjectProductRelationByIds(ids);
        }
        subjectIds.forEach(subjectId -> {
            CmsSubjectProductRelation cmsSubjectProductRelation = new CmsSubjectProductRelation();
            cmsSubjectProductRelation.setSubjectId(subjectId);
            cmsSubjectProductRelation.setProductId(product.getId());
            cmsSubjectProductRelationMapper.insertCmsSubjectProductRelation(cmsSubjectProductRelation);
        });
        return pmsProductMapper.updatePmsProduct(product);
    }

    /**
     * 批量删除商品
     *
     * @param ids 需要删除的商品ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePmsProductByIds(Long[] ids)
    {
        for (Long id : ids) {
            PmsProduct pmsProduct = pmsProductMapper.selectPmsProductById(id);
            if (pmsProduct.getPromotionType() == 2) {
                PmsMemberPrice pmsMemberPrice = new PmsMemberPrice();
                pmsMemberPrice.setProductId(id);
                Long[] mIds = pmsMemberPriceMapper.selectPmsMemberPriceList(pmsMemberPrice)
                        .stream().map(PmsMemberPrice::getId).toArray(Long[]::new);
                if (mIds.length > 0) {
                    pmsMemberPriceMapper.deletePmsMemberPriceByIds(mIds);
                }
            } else if (pmsProduct.getPromotionType() == 3) {
                PmsProductLadder pmsProductLadder = new PmsProductLadder();
                pmsProductLadder.setProductId(id);
                Long[] lIds = pmsProductLadderMapper.selectPmsProductLadderList(pmsProductLadder)
                        .stream().map(PmsProductLadder::getId).toArray(Long[]::new);
                if (lIds.length > 0) {
                    pmsProductLadderMapper.deletePmsProductLadderByIds(lIds);
                }
            } else if (pmsProduct.getPromotionType() == 4) {
                PmsProductFullReduction pmsProductFullReduction = new PmsProductFullReduction();
                pmsProductFullReduction.setProductId(id);
                Long[] fIds = pmsProductFullReductionMapper.selectPmsProductFullReductionList(pmsProductFullReduction)
                        .stream().map(PmsProductFullReduction::getId).toArray(Long[]::new);
                if (fIds.length > 0) {
                    pmsProductFullReductionMapper.deletePmsProductFullReductionByIds(fIds);
                }
            }
            PmsProductAttributeValue pmsProductAttributeValue = new PmsProductAttributeValue();
            pmsProductAttributeValue.setProductId(id);
            Long[] attrIds = pmsProductAttributeValueMapper.selectPmsProductAttributeValueList(pmsProductAttributeValue)
                    .stream().map(PmsProductAttributeValue::getId).toArray(Long[]::new);
            if (attrIds.length > 0) {
                pmsProductAttributeValueMapper.deletePmsProductAttributeValueByIds(attrIds);
            }
            PmsProductSku pmsProductSku = new PmsProductSku();
            pmsProductSku.setProductId(id);
            Long[] skuIds = pmsProductSkuMapper.selectPmsProductSkuList(pmsProductSku)
                    .stream().map(PmsProductSku::getId).toArray(Long[]::new);
            if (skuIds.length > 0) {
                pmsProductSkuMapper.deletePmsProductSkuByIds(skuIds);
            }
            CmsSubjectProductRelation subjectProductRelation = new CmsSubjectProductRelation();
            subjectProductRelation.setProductId(id);
            Long[] rIds = cmsSubjectProductRelationMapper.selectCmsSubjectProductRelationList(subjectProductRelation)
                    .stream().map(CmsSubjectProductRelation::getId).toArray(Long[]::new);
            if (rIds.length > 0) {
                cmsSubjectProductRelationMapper.deleteCmsSubjectProductRelationByIds(rIds);
            }
        }
        return pmsProductMapper.deletePmsProductByIds(ids);
    }

    /**
     * 删除商品信息
     *
     * @param id 商品ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePmsProductById(Long id)
    {
        PmsProduct pmsProduct = pmsProductMapper.selectPmsProductById(id);
        if (pmsProduct.getPromotionType() == 2) {
            PmsMemberPrice pmsMemberPrice = new PmsMemberPrice();
            pmsMemberPrice.setProductId(id);
            Long[] mIds = pmsMemberPriceMapper.selectPmsMemberPriceList(pmsMemberPrice)
                    .stream().map(PmsMemberPrice::getId).toArray(Long[]::new);
            if (mIds.length > 0) {
                pmsMemberPriceMapper.deletePmsMemberPriceByIds(mIds);
            }
        } else if (pmsProduct.getPromotionType() == 3) {
            PmsProductLadder pmsProductLadder = new PmsProductLadder();
            pmsProductLadder.setProductId(id);
            Long[] lIds = pmsProductLadderMapper.selectPmsProductLadderList(pmsProductLadder)
                    .stream().map(PmsProductLadder::getId).toArray(Long[]::new);
            if (lIds.length > 0) {
                pmsProductLadderMapper.deletePmsProductLadderByIds(lIds);
            }
        } else if (pmsProduct.getPromotionType() == 4) {
            PmsProductFullReduction pmsProductFullReduction = new PmsProductFullReduction();
            pmsProductFullReduction.setProductId(id);
            Long[] fIds = pmsProductFullReductionMapper.selectPmsProductFullReductionList(pmsProductFullReduction)
                    .stream().map(PmsProductFullReduction::getId).toArray(Long[]::new);
            if (fIds.length > 0) {
                pmsProductFullReductionMapper.deletePmsProductFullReductionByIds(fIds);
            }
        }
        PmsProductAttributeValue pmsProductAttributeValue = new PmsProductAttributeValue();
        pmsProductAttributeValue.setProductId(id);
        Long[] attrIds = pmsProductAttributeValueMapper.selectPmsProductAttributeValueList(pmsProductAttributeValue)
                .stream().map(PmsProductAttributeValue::getId).toArray(Long[]::new);
        if (attrIds.length > 0) {
            pmsProductAttributeValueMapper.deletePmsProductAttributeValueByIds(attrIds);
        }
        PmsProductSku pmsProductSku = new PmsProductSku();
        pmsProductSku.setProductId(id);
        Long[] skuIds = pmsProductSkuMapper.selectPmsProductSkuList(pmsProductSku)
                .stream().map(PmsProductSku::getId).toArray(Long[]::new);
        if (skuIds.length > 0) {
            pmsProductSkuMapper.deletePmsProductSkuByIds(skuIds);
        }
        CmsSubjectProductRelation subjectProductRelation = new CmsSubjectProductRelation();
        subjectProductRelation.setProductId(id);
        Long[] rIds = cmsSubjectProductRelationMapper.selectCmsSubjectProductRelationList(subjectProductRelation)
                .stream().map(CmsSubjectProductRelation::getId).toArray(Long[]::new);
        if (rIds.length > 0) {
            cmsSubjectProductRelationMapper.deleteCmsSubjectProductRelationByIds(rIds);
        }
        return pmsProductMapper.deletePmsProductById(id);
    }

    /**
     * 修改商品状态
     *
     * @param map 参数
     * @return 结果
     */
    @Override
    public int updatePmsProductStatus(Map<String, Integer> map) {
        return pmsProductMapper.updatePmsProductStatus(map);
    }
}